#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "kp_base_type.h"
#include "wifi_sta.h"
#include "iot_fan_key.h"
#include "iot_fan_infrared.h"
#include "iot_fan_led.h"
#include "iot_fan_motor.h"
#include "iot_fan_cloud_mqtt.h"

#include "oled_GME12864.h"
#include "oled_gui.h"

#include "iot_spi.h"



#define TASK_DELAY 200 //

#define CLOUD_TASK_STACK_SIZE (1024 * 10)
#define CLOUD_TASK_PRIO 24


/* 温湿度传感器处理任务，周期查询温度和湿度数据 */
void AgricultureShtTask(void)
{
    ShtIICInit();
	//osDelay(TASK_DELAY);
	SHT3X_init();
	osDelay(TASK_DELAY);
	
    /* 初始化变量 */
    float temperature = 0.0;
	float humidity = 0.0;
	static motor_speed_level motorSpeed = MOTOR_SPEED_LEVEL_0;
	
    while (1) {
		/* 查询温度和湿度数据 */
	    SHT3X_ReadMeasurementBuffer(&temperature, &humidity);
		app_msg_t* app_msg = malloc(sizeof(app_msg_t));
        if (NULL != app_msg) {
            app_msg->msg_type = en_msg_report;
            app_msg->msg.report.hum = (int)humidity;
            app_msg->msg.report.temp = (int)temperature;
            if (osMessageQueuePut(g_app_cb.app_msg, &app_msg, 0U, CONFIG_QUEUE_TIMEOUT != 0)) {
                free(app_msg);
            }
        }

		//hi_watchdog_feed();
	    printf("temperature:%.2f, humidity:%.2f \r\n", temperature, humidity);
	    if (humidity > 80.0) {
			if (motorSpeed != MOTOR_SPEED_LEVEL_3) {
    			AgricultureMotorCtrl(MOTOR_SPEED_LEVEL_3);
				motorSpeed = MOTOR_SPEED_LEVEL_3;
    			printf("adjust motor speed to level 3 \r\n");
			}
	    } else if (humidity > 70.0) {
	        if (motorSpeed != MOTOR_SPEED_LEVEL_2) {
    			AgricultureMotorCtrl(MOTOR_SPEED_LEVEL_2);
				motorSpeed = MOTOR_SPEED_LEVEL_2;
    			printf("adjust motor speed to level 2 \r\n");
	        }
	    } else if (humidity > 60.0) {
			if (motorSpeed != MOTOR_SPEED_LEVEL_1) {
    			AgricultureMotorCtrl(MOTOR_SPEED_LEVEL_1);
				motorSpeed = MOTOR_SPEED_LEVEL_1;
    			printf("adjust motor speed to level 1 \r\n");
			}
	    } else {
	        if (motorSpeed != MOTOR_SPEED_LEVEL_0) {
    			AgricultureMotorCtrl(MOTOR_SPEED_LEVEL_0);
    			motorSpeed = MOTOR_SPEED_LEVEL_0;
    			printf("adjust motor speed to level 0 \r\n");
	        }
	    }

         // 更新 OLED 显示
        UpdateOLEDDisplay(temperature, humidity);

		osDelay(TASK_DELAY);
    }

    return;
}

// 定义一个函数来更新 OLED 显示
void UpdateOLEDDisplay(float temperature, float humidity)
{
     OLED_Init();               //初始化OLED  

	/* 清屏（全黑） */
    OLED_Clear(0);

    // 构造字符串
    char tempStr[32];
    char humiStr[32];
    snprintf(tempStr, sizeof(tempStr), "Temp: %.2f°C", temperature);
    snprintf(humiStr, sizeof(humiStr), "Humi: %.2f%%", humidity);

    // 显示温度和湿度
    printf("start display \r\n");
    GUI_ShowString(0, 0, (u8*)tempStr, 16, 1); // 确保使用 u8* 类型的指针
    GUI_ShowString(0, 16, "bb", 16, 1); // 确保使用 u8* 类型的指针
    msleep(1500);
    OLED_Clear(0);
    printf("stop display \r\n");

    // 更新 OLED 显示
    // 如果您的库需要调用特定的更新屏幕函数，请在这里调用
    // GUI_UpdateScreen();
}

void Iot_Fan_demo(void)
{
    /* 各模块初始化 */
    //AgricultureKeyInit();
	AgricultureLedInit();
	AgricultureMotorInit();
	//ShtIICInit();
	//SHT3X_init();
	//osDelay(TASK_DELAY);

	/* 设置线程属性 */
    osThreadAttr_t attr;
    attr.name = "AgricultureKeyTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024 * 4;
    attr.priority = 25;

    /* 创建按键控制风扇线程 */
    /*osThreadId_t threadIDKey = osThreadNew((osThreadFunc_t)AgricultureKeyTask, NULL, &attr);
    if (threadIDKey == NULL)
    {
        printf("Falied to create AgricultureKeyTask!\r\n");
    } else {
		printf("success to create AgricultureKeyTask!\r\n");
    }*/

    /* 创建红外传感器控制LED线程 */
	attr.name = "InfraredCtrlTask";
    osThreadId_t threadIDLed = osThreadNew((osThreadFunc_t)AgricultureInfraredTask, NULL, &attr);
    if (threadIDLed == NULL)
    {
        printf("Falied to create InfraredCtrlTask!\r\n");
    } else {
		printf("success to create InfraredCtrlTask!\r\n");
    }
	
    /* 创建温湿度处理线程 */
	attr.name = "AgricultureShtTask";
    osThreadId_t threadIDSht = osThreadNew((osThreadFunc_t)AgricultureShtTask, NULL, &attr);
    if (threadIDSht == NULL)
    {
        printf("Falied to create AgricultureShtTask!\r\n");
    } else {
		printf("success to create AgricultureShtTask!\r\n");
    }
	
	/* 创建设备上云处理线程 */
    osDelay(TASK_DELAY);
	attr.name = "AgricultureCloudTask";
    attr.stack_size = CLOUD_TASK_STACK_SIZE;
    attr.priority = CLOUD_TASK_PRIO;
    osThreadId_t threadIDCloud = osThreadNew((osThreadFunc_t)AgricultureCloudTask, NULL, &attr);
    if (threadIDCloud == NULL)
    {
        printf("Falied to create AgricultureCloudTask!\r\n");
    } else {
		printf("success to create AgricultureCloudTask!\r\n");
    }
}

APP_FEATURE_INIT(Iot_Fan_demo);

